﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace zielonka.co.uk.extensions.system.io
{
    // Description:	This method extends the DirectoryInfo class to return the size in bytes of the directory represented by the DirectoryInfo instance.
    /*
        DirectoryInfo WindowsDir = new DirectoryInfo(@"C:\WINDOWS");
        long WindowsSize = WindowsDir.GetSize();
     */

    public static partial class DirectoryInfoExtensions
    {
        public static long GetSize(this DirectoryInfo dir)
        {
            long length = 0;

            // Loop through files and keep adding their size
            foreach (FileInfo nextfile in dir.GetFiles())
                length += nextfile.Exists ? nextfile.Length : 0;

            // Loop through subdirectories and keep adding their size
            foreach (DirectoryInfo nextdir in dir.GetDirectories())
                length += nextdir.Exists ? nextdir.GetSize() : 0;

            return length;
        }
    }
}
